/**
 * 
 */
package com.pkdl.tables;

import java.awt.Image;
import java.util.List;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import com.pkdl.dto.PhieuKham_ThuocDTO;
import com.pkdl.forms.DlgQuanLyDichVu;

/**
 * @author Administrator
 * 
 */
public class TableCapNhatThemThuoc extends TableBase {
	private List<PhieuKham_ThuocDTO> lstItem;
	private JTable table;

	public TableCapNhatThemThuoc(List<PhieuKham_ThuocDTO> _lstItem) {
		super();
		this.lstItem = _lstItem;
	}

	private void addElementIntoTable(List<PhieuKham_ThuocDTO> lstItem) {
		// arraylsit chua tat cac cac file tuong ung voi thu muc
		// co duong dan strTempPathLeft
		this.arrRow = new Vector<Object>();
		// ///////////////
		for (int i = 0; i < lstItem.size(); i++) {
			JLabel lb = new JLabel();
			String pathImages = "/com/pkdl/resources/vcard.png";

			ImageIcon imageIcon = new ImageIcon(
					DlgQuanLyDichVu.class.getResource(pathImages));

			if (imageIcon.getIconWidth() > this.PREFERRED_SIZE
					|| imageIcon.getIconHeight() > this.PREFERRED_SIZE) {
				imageIcon = new ImageIcon(imageIcon.getImage()
						.getScaledInstance(this.PREFERRED_SIZE,
								this.PREFERRED_SIZE, Image.SCALE_SMOOTH));
			}

			lb.setIcon(imageIcon);
			lb.setName(String.valueOf(lstItem.get(i).getMaThuoc()));
			lb.setHorizontalAlignment(SwingConstants.CENTER);
			// ///
			// them cac phan tu vao mang
			Vector<Object> a = new Vector<Object>();
			a.add(lb);// add icon
			a.add(lstItem.get(i).getMaThuoc());
			a.add(lstItem.get(i).getTenThuoc());
			a.add(lstItem.get(i).getSoLuong());

			this.arrRow.add(a);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.pkdl.tables.TableBase#loadDataIntoTableBook(javax.swing.JTable)
	 */
	public void loadDataIntoTable(JTable table) {
		this.table = table;
		// TODO Auto-generated method stub
		table.setRowHeight(18);
		this.arrCol = new Vector<String>();
		this.columnNames = new String[] { "", "MaThuoc", "TenThuoc", "SoLuong" };
		for (String strNameCol : this.columnNames) {
			this.arrCol.add(strNameCol);
		}

		this.addElementIntoTable(this.lstItem);
		// them dong, cot
		MyTableModel model = new MyTableModel(this.arrRow, this.arrCol, false);
		table.setModel(model);
		// thuc hien sort column theo tung loai (thu muc, file)
		// thu muc tang tu tren xuong duoi
		// sau do den file tang tu tren xuong duoi
		table.setAutoCreateColumnsFromModel(false);
		this.sortAllRowByFullName(model, 2, true);
		// /////////////
		this.editTable(table);
	}

	/**
	 * Refresh data
	 */
	public void refreshTable() {
		this.addElementIntoTable(this.lstItem);
		// them dong, cot
		MyTableModel model = new MyTableModel(this.arrRow, this.arrCol, false);
		this.table.setModel(model);
	}
}
